#设置读取文件的工作路径
setwd("D:/1大学生活/大三下/zxy/R语言/ADdata")
#导入txt
ADdata1 <- read.delim("ADdata1.txt",row.names = NULL)
#导入csv
ADdata2 <- read.csv("ADdata2.csv")

install.packages("openxlsx")
library(openxlsx)
#导入xlsx
ADdata3 <- read.xlsx("ADdata3.xlsx")
#ADdata4以“ ”为分隔符
ADdata4 <- read.table("ADdata4.txt",header = TRUE,row.names = NULL)

#将表头统一
colnames(ADdata1)[1] = colnames(ADdata2)[1] = colnames(ADdata3)[1] = colnames(ADdata4)[1] = "ID"

#用merge函数将表合并
merged_data=merge(ADdata1,ADdata2,by="ID")
merged_data=merge(merged_data,ADdata3,by="ID")
merged_data=merge(merged_data,ADdata4,by="ID")


#观察merged_data发现，表中有重复值，想要对比重复的两列有什么，但是不太会，所以直接观察后得出相同
# 保留带 .x 的列和其他不重复列
merged_clean <- merged_data[, !grepl("\\.y$", colnames(merged_data))]

# 去掉 .x 后缀
colnames(merged_clean) <- gsub("\\.x$", "", colnames(merged_clean))

setwd("D:/1大学生活/大三下/zxy/R语言/实践3")
dir.create("ADdata_table")
write.csv(merged_clean,file = "ADdata_table/ADdate.csv",row.names = FALSE)
write.xlsx(merged_clean,file = "ADdata_table/ADdata.xlsx",rownames = FALSE)
write.table(merged_clean,file = "ADdata_table/ADdata.txt",sep = "\t",row.names = FALSE)

            